package com.bestcem.xm.ticket.controller.param;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.ticket.constant.ColumnConstant;
import com.bestcem.xm.ticket.entity.mongo.Notification;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version 通知提醒新增微信公众号需求
 * @date 2021/8/23 15:04
 * @date 2022/4/7 12:16 XiaoYunSong 通知提醒新增微信公众号需求 新增 是否开启免打扰/是否开启微信公众号通知
 */
@Data
public class NotificationParam {

    private Rule rule;

    private Way ways;

    /**
     * 免打扰规则
     */
    @JsonProperty(value = ColumnConstant.Notification.NO_DISTURB)
    private List<String> noDisturb;

    /**
     * 是否开启免打扰
     * 通知提醒新增微信公众号需求 新增字段
     */
    @JsonProperty("no_disturb_enable")
    private Boolean noDisturbEnable;

    @Data
    public static class Rule {
        /**
         * everySCRspdEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.EVERYSCRSPDENABLE)
        private Boolean everyRspdEnable;

        /**
         * sCRspdReachEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDREACHENABLE)
        private Boolean reachEnable;

        /**
         * sCRspdReachCount
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDREACHCOUNT)
        private Integer reachCount;

        /**
         * sCRspdFreqEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDFREQENABLE)
        private Boolean freqEnable;

        /**
         * sCRspdFreqTime
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDFREQTIME)
        private String freqTime;

        /**
         * sCRspdFreqOperator
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDFREQOPERATOR)
        private String freqOperator;

        /**
         * sCRspdFreqValue
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDFREQVALUE)
        private Integer freqValue;

        /**
         * sCRspdRatioEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDRATIOENABLE)
        private Boolean ratioEnable;

        /**
         * sCRspdRatioTime
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDRATIOTIME)
        private String ratioTime;

        /**
         * sCRspdRatioOperator
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDRATIOOPERATOR)
        private String ratioOperator;

        /**
         * sCRspdRatioValue
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.SCRSPDRATIOVALUE)
        private Integer ratioValue;

        /**
         * quoteFullEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.QUOTEFULLENABLE)
        private Boolean quoteFullEnable;

        /**
         * awardBonusEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.AWARDBONUSENABLE)
        private Boolean awardBonusEnable;

        /**
         * awardDrawEnable
         */
        @JsonProperty(value = ColumnConstant.Notification.Rule.AWARDDRAWENABLE)
        private Boolean awardDrawEnable;
    }

    @Data
    public static class Way {

        /**
         * 邮箱列表
         */
        @JsonProperty(value = ColumnConstant.Notification.Way.EMAIL)
        private List<String> emails;

        /**
         * 是否开启邮箱通知
         */
        @JsonProperty(value = ColumnConstant.Notification.Way.EMAIL_ENABLE)
        private Boolean emailEnable;

        /**
         * 手机号列表
         */
        @JsonProperty(value = ColumnConstant.Notification.Way.MOBILE)
        private List<String> mobiles;

        /**
         * 是否开启手机通知
         */
        @JsonProperty(value = ColumnConstant.Notification.Way.MOBILE_ENABLE)
        private Boolean mobileEnable;

        /**
         * 是否开启微信公众号通知 通知提醒新增微信公众号需求 新增字段
         */
        @JsonProperty("wechat_public_enable")
        private Boolean wechatPublicEnable;
    }

    /**
     * NotificationParam.Way -> Notification.Way
     *
     * @param way NotificationParam.Way
     * @return Notification.Way
     */
    public static Notification.Way wayToEntityWay(Way way) {
        if (way == null) {
            return null;
        }

        Notification.Way way1 = new Notification.Way();

        List<String> list = way.getEmails();
        if (CollUtil.isNotEmpty(list)) {
            List<String> emails = new ArrayList<>(list.size());

            for (String s : list) {
                if ("null".equals(s) || StringUtils.isBlank(s)) {
                    continue;
                }
                emails.add(s);
            }
            way1.setEmails(emails);
        }
        way1.setEmailEnable(way.getEmailEnable());

        List<String> list1 = way.getMobiles();
        if (CollUtil.isNotEmpty(list1)) {
            List<String> mobiles = new ArrayList<>(list1.size());

            for (String s : list1) {
                if ("null".equals(s) || StringUtils.isBlank(s)) {
                    continue;
                }
                mobiles.add(s);
            }
            way1.setMobiles(mobiles);
        }
        way1.setMobileEnable(way.getMobileEnable());

        // 是否开启微信公众号通知 通知提醒新增微信公众号需求 新增字段
        way1.setWechatPublicEnable(way.getWechatPublicEnable());

        return way1;
    }

    /**
     * NotificationParam.Rule -> Notification.Rule
     *
     * @param rule NotificationParam.Rule
     * @return Notification.Rule
     */
    public static Notification.Rule ruleToEntityRule(Rule rule) {
        if (rule == null) {
            return null;
        }

        Notification.Rule rule1 = new Notification.Rule();

        rule1.setEveryRspdEnable(rule.getEveryRspdEnable());
        rule1.setReachEnable(rule.getReachEnable());
        rule1.setReachCount(rule.getReachCount());
        rule1.setFreqEnable(rule.getFreqEnable());
        rule1.setFreqTime(rule.getFreqTime());
        rule1.setFreqOperator(rule.getFreqOperator());
        rule1.setFreqValue(rule.getFreqValue());
        rule1.setRatioEnable(rule.getRatioEnable());
        rule1.setRatioTime(rule.getRatioTime());
        rule1.setRatioOperator(rule.getRatioOperator());
        rule1.setRatioValue(rule.getRatioValue());
        rule1.setQuoteFullEnable(rule.getQuoteFullEnable());
        rule1.setAwardBonusEnable(rule.getAwardBonusEnable());
        rule1.setAwardDrawEnable(rule.getAwardDrawEnable());

        return rule1;
    }

}
